//
// Created by wkm on 2024-03-17.
//

#include "MixColumns.h"

//定义列混合操作
void MixColumns(unsigned char *col) {
    unsigned char tmp[4], xt[4];
    int i;
    for (i = 0; i < 4; i++, col += 4) {  //col代表一列的基地址，col+4:下一列的基地址
        //xt[n]代表*2   xt[n]^col[n]代表*3   col[n]代表*1
        tmp[0] = x2time(col[0]) ^ x3time(col[1]) ^ col[2] ^ col[3];    //2 3 1 1

        tmp[1] = col[0] ^ x2time(col[1]) ^ x3time(col[2]) ^ col[3];    //1 2 3 1
        tmp[2] = col[0] ^ col[1] ^ x2time(col[2]) ^ x3time(col[3]);    //1 1 2 3
        tmp[3] = x3time(col[0]) ^ col[1] ^ col[2] ^ x2time(col[3]);    //3 1 1 2
        //修改后的值 直接在原矩阵上修改
        col[0] = tmp[0];
        col[1] = tmp[1];
        col[2] = tmp[2];
        col[3] = tmp[3];
    }
}

//定义逆向列混淆
void Contrary_MixColumns(unsigned char *col) {
    unsigned char tmp[4];
    unsigned char xt2[4];//colx2
    unsigned char xt4[4];//colx4
    unsigned char xt8[4];//colx8
    int x;
    for (x = 0; x < 4; x++, col += 4) {
        tmp[0] = xEtime(col[0]) ^ xBtime(col[1]) ^ xDtime(col[2]) ^ x9time(col[3]); // E B D 9
        tmp[1] = x9time(col[0]) ^ xEtime(col[1]) ^ xBtime(col[2]) ^ xDtime(col[3]); // 9 E B D
        tmp[2] = xDtime(col[0]) ^ x9time(col[1]) ^ xEtime(col[2]) ^ xBtime(col[3]); // D 9 E B
        tmp[3] = xBtime(col[0]) ^ xDtime(col[1]) ^ x9time(col[2]) ^ xEtime(col[3]); // B D 9 E
        col[0] = tmp[0];
        col[1] = tmp[1];
        col[2] = tmp[2];
        col[3] = tmp[3];
    }
}
